
#include "e_mod_main.h"
#ifdef HAVE_XMMSCLIENT_XMMSCLIENT_H
#include <xmmsclient/xmmsclient.h>

static int _xmms2_connect(Eloquence_Face *face);
static void _do_reljump(xmmsc_connection_t *conn, int where);
static void _xmms2_pause(xmmsc_connection_t *xmms2);
static void _xmms2_play(xmmsc_connection_t *xmms2);

void xmms2_init_menu(E_Menu *menu, Eloquence_Face *face)
{
	E_Menu_Item *mi;

	mi = e_menu_item_new(menu);
	e_menu_item_label_set(mi, _("Play/Pause"));
	e_menu_item_callback_set(mi, xmms2_control_play_pause, face);
	mi = e_menu_item_new(menu);
	e_menu_item_label_set(mi, _("Next Song"));
	e_menu_item_callback_set(mi, xmms2_control_track_next, face);
	mi = e_menu_item_new(menu);
	e_menu_item_label_set(mi, _("Previous Song"));
	e_menu_item_callback_set(mi, xmms2_control_track_prev, face);
	mi = e_menu_item_new(menu);
	e_menu_item_label_set(mi, _("Shuffle Songs"));
	e_menu_item_callback_set(mi, xmms2_control_shuffle, face);
	mi = e_menu_item_new(menu);
	e_menu_item_label_set(mi, _("Sort Songs"));
	e_menu_item_callback_set(mi, xmms2_control_sort, face);
}

void xmms2_connect(Eloquence_Face *face)
{
	if (_xmms2_connect(face) != 0 )
		face->recheck_timer = ecore_timer_add(SEARCH_REFRESH, _xmms2_connect, face);
}

void xmms2_disconnect(Eloquence_Face *face)
{
	if (face->session.ptr)
	{
		xmmsc_unref(face->session.ptr);
		face->session.ptr = NULL;
	}

	if (face->recheck_timer)
	{
		ecore_timer_del(face->recheck_timer);
		face->recheck_timer = NULL;
	}
}

int xmms2_update_song(Eloquence_Face *face)
{
	xmmsc_connection_t *xmms2 = face->session.ptr;

	if (xmms2) {
		guint id;
		xmmsc_result_t *res;

		res = xmmsc_playback_current_id(xmms2);
		xmmsc_result_wait(res);
		if (!xmmsc_result_get_uint(res, &id))
			printf("Eloquence: XMMS2: update song broken result\n");

		xmmsc_result_unref(res);

		res = xmmsc_medialib_get_info(xmms2, id);
		xmmsc_result_wait(res);

		xmmsc_entry_format(face->outbuf, sizeof(face->outbuf), "${artist} - ${title}", res);

		xmmsc_result_unref(res); 

		face->song = face->outbuf;
		draw_text(face);
	}

	return 1;
}

void xmms2_control_play_pause(Eloquence_Face *face)
{
	xmmsc_connection_t *xmms2 = face->session.ptr;

	if (xmms2) {
		guint status;
		xmmsc_result_t *res;

		res = xmmsc_playback_status(xmms2);
		xmmsc_result_wait(res);

		if (!xmmsc_result_get_uint(res, &status))
			printf("Eloquence: XMMS2: playback status broken result\n");

		if (status == 1)
			_xmms2_pause(xmms2);
		else
			_xmms2_play(xmms2);
		xmmsc_result_unref(res);
	}
}

void xmms2_control_track_next(Eloquence_Face *face)
{
	if (face->session.ptr) {
		_do_reljump(face->session.ptr, 1);
		xmms2_update_song(face);
	}
}

void xmms2_control_track_prev(Eloquence_Face *face)
{
	if (face->session.ptr) {
		_do_reljump(face->session.ptr, -1);
		xmms2_update_song(face);
	}
}

void xmms2_control_shuffle(Eloquence_Face *face)
{
	if (face->session.ptr) {
		xmmsc_result_t *res;

		res = xmmsc_playlist_shuffle(face->session.ptr);
		xmmsc_result_wait(res);
		xmmsc_result_unref(res);
	}
}

void xmms2_control_sort(Eloquence_Face *face)
{
	if (face->session.ptr) {
		xmmsc_result_t *res;

		res = xmmsc_playlist_sort(face->session.ptr, "artist");
		xmmsc_result_wait(res);
		xmmsc_result_unref(res);
	}
}

void xmms2_add_file_callback(Eloquence_Face *face, 
		Evas_Object *obj, const char *emission, const char *source)
{
	if (face->session.ptr) {
		/* Empty now, for drag'n'drop of files to play */
	}
}


static int _xmms2_connect(Eloquence_Face *face)
{
	char *path;

	xmmsc_connection_t *xmms2 = xmmsc_init("eloquence");

	path = getenv("XMMS_PATH");
	if (!xmmsc_connect(xmms2, path)) {
		printf("Eloquence: XMMS2: Could not connect to xmms2d: %s",  xmmsc_get_last_error(xmms2));
		xmmsc_unref(xmms2);
		xmms2 = NULL;
	}

	face->session.ptr = xmms2;

	// return false on success to stop looping the recheck timer
	return xmms2 == NULL;
}

static void _do_reljump (xmmsc_connection_t *conn, int where)
{
	xmmsc_result_t *res;
        
	res = xmmsc_playlist_set_next_rel(conn, where);
	xmmsc_result_wait(res);
                    
	if (xmmsc_result_iserror(res)) {
		printf("Eloquence: XMMS2: Couldn't advance in playlist: %s\n", xmmsc_result_get_error(res));
		return;
	}
	xmmsc_result_unref(res);
                
	res = xmmsc_playback_tickle(conn);
	xmmsc_result_wait(res);
	xmmsc_result_unref(res);
}               

static void _xmms2_pause(xmmsc_connection_t *xmms2)
{
	xmmsc_result_t *res;
	res = xmmsc_playback_pause(xmms2);
	xmmsc_result_wait(res); 
	if (xmmsc_result_iserror(res))
		printf("Eloquence: XMMS2: Couldn't pause playback: %s\n", xmmsc_result_get_error(res));
	xmmsc_result_unref(res);
}

static void _xmms2_play(xmmsc_connection_t *xmms2)
{
	xmmsc_result_t *res;
	res = xmmsc_playback_start(xmms2);
	xmmsc_result_wait(res);
	if (xmmsc_result_iserror(res))
		printf("Eloquence: XMMS2: Couldn't start playback: %s\n", xmmsc_result_get_error(res));
	xmmsc_result_unref(res);
}

#endif // HAVE_XMMSCLIENT_XMMSCLIENT_H


